$(function () {
    var options = {
        modalName: "子订单",
        url: "/render/orderItemManager/list",
        getInfoUrl: "/render/orderItemManager/get/{id}",
        updateUrl: "/render/orderItemManager/edit",
        removeUrl: "/render/orderItemManager/remove",
        createUrl: "/render/orderItemManager/add",
        searchUrl: "/render/orderItemManager/search",
        getInfoUrlByComment:"/render/commentManager/getByOrderItem/{id}",
        updateUrlByComment:"/render/commentManager/editByOrderItem",
        pageNumber:1,
        pageSize:7,
        columns: [
            {
                checkbox: true,
                formatter:checkboxMatter
            }, {
                field: 'user_id',
                title: '用户ID',
                align: "center",
                width: '50px',
                class:"colStyle",
                sortable : true,
                cellStyle:formatTableUnit,
                formatter:paramsMatter
            }, {
                field: 'order_no',
                title: '订单号',
                align: "center",
                width: '100px',
                sortable : true,
                cellStyle:formatTableUnit,
                formatter:paramsMatter
            }, {
                field: 'product_id',
                title: '商品ID',
                align: "center",
                width: '50px',
                class:"colStyle",
                sortable : true,
                formatter:paramsMatter
            }, {
                field: 'product_name',
                title: '商品名',
                align: "center",
                width: '50px',
                cellStyle:formatTableUnit,
                formatter:paramsMatter
            }, {
                field: 'product_image',
                title: '商品图',
                align: "center",
                width: '50px',
                cellStyle:formatTableUnit,
                formatter: function paramsMatter(value) {
                    var span=document.createElement('span');
                    span.setAttribute('title',value);
                    var html="<img src='"+value+"' class='img-responsive img-rounded auto-shake'>";
                    span.innerHTML = html;
                    return span.outerHTML;
                }
            }, {
                field: 'current_unit_price',
                title: '单价',
                align: "center",
                width: '50px',
                sortable : true,
                cellStyle:formatTableUnit,
                formatter:paramsMatter
            }, {
                field: 'quantity',
                title: '件数',
                align: "center",
                width: '50px',
                sortable : true,
                cellStyle:formatTableUnit,
                formatter:paramsMatter
            }, {
                field: 'shop_status',
                title: '状态',
                align: "center",
                width: '50px',
                sortable : true,
                cellStyle:formatTableUnit,
                // formatter:paramsMatter
                formatter: function (value) {
                    if (value == 0) {
                        value = '退单';
                    }

                    if (value == 10) {
                        value = '未付款'
                    }

                    if (value == 40){
                        value = '发货';
                    }

                    if (value == 50){
                        value = '交易完成';
                    }

                    if (value == 30){
                        value = '未发货';
                    }

                    if (value == 60){
                        value = '交易结束';
                    }

                    var span=document.createElement('span');
                    span.setAttribute('title',value);
                    span.innerHTML = value;
                    return span.outerHTML;
                }
            }, {
                field: 'create_time',
                title: '创建时间',
                width: '88px',
                align: "center",
                sortable : true,
                cellStyle:formatTableUnit,
                formatter:paramsMatter
            }, {
                field: 'operate',
                title: '操作',
                align: "center",
                width: '150px',
                sortable : true,
                formatter: function operateFormatter(code, row) {
                    var trId = row.id;
                    var operateBtn = [
                        '<a class="btn btn-sm btn-success btn-reply" data-id="' + trId + '" title="回复">回复</a></div>',
                        '<a class="btn btn-sm btn-success btn-update" data-id="' + trId + '" title="编辑"><i class="fa fa-edit fa-fw"></i></a></div>',
                        '<a class="btn btn-sm btn-danger btn-remove" data-id="' + trId + '" title="删除"><i class="fa fa-trash-o fa-fw"></i></a></div>'
                    ];
                    return operateBtn.join('');
                }
            }
        ]
    };

    //search按钮绑定查询事件
    $('#btnSearch').on('click', function () {
        var searchtext = {
            user_id: $("#search-user_id").val(),
            order_no: $("#search-order_no").val(),
            product_name: $("#search-product_name").val()
        };

        if($("#search-user_id").val() === ""&&$("#search-order_no").val() === ""&&$("#search-product_name").val() === ""){
            postParams(options.url,options.onExpandRow,options,options.rowStyle,options.columns,null,true);
        }else {
            postParams(options.searchUrl,options.onExpandRow,options,options.rowStyle,options.columns,JSON.stringify(searchtext),false);
        }
    });

    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for(var i = 0; i < strs.length; i ++) {
                theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

    if (GetRequest().flag!=null&&GetRequest().flag!==undefined&&GetRequest().flag!==''&&GetRequest().flag) {
            // 初始化table组件
            var table = new Table(options);
            table.init();
            $("#search-order_no").val(GetRequest().order_no);
            $('#btnSearch').click();
            GetRequest().flag=false;
    }else {
        // 初始化table组件
        var table = new Table(options);
        table.init();
    }

    $(".close,.btn-default").on("click",function () {
        $(".modal").css("display","");
        $(".modal").removeClass("in");
        $(".modal-backdrop").removeClass("in");
    });
});